package cn.kgc.easybuy.service.impl;

import cn.kgc.easybuy.dao.AddressMapper;
import cn.kgc.easybuy.pojo.Address;
import cn.kgc.easybuy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-16 17:06
 **/
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> selectAllAddress() {
        return addressMapper.selectAllAddress();
    }

    @Override
    public List<Address> selectAddressByUserId(int userId) {
        return addressMapper.selectAddressByUserId(userId);
    }

    @Override
    public Address selectAddressById(int id) {
        return addressMapper.selectAddressById(id);
    }

    @Override
    public boolean addAddress(Address address) {
        if (address.getIsDefault() == 1) {
            addressMapper.updateDefaultAddress(address.getUserId());
        }
        return addressMapper.addAddress(address) > 0;
    }

    @Override
    public boolean updateAddress(Address address) {
        if (address.getIsDefault() == 1) {
            addressMapper.updateDefaultAddress(address.getUserId());
        }
        return addressMapper.updateAddress(address) > 0;
    }

    @Override
    public boolean deleteAddressById(int id) {
        return addressMapper.deleteAddressById(id) > 0;
    }

    @Override
    public Address selectDefaultAddress(int userId) {
        return addressMapper.selectDefaultAddress(userId);
    }
}
